* s~\t+$~~
[lhc/web/wiklou.git] / maintenance / commandLine.inc
1 <?php
2 /**
3 * @todo document
4 * @package MediaWiki
5 * @subpackage Maintenance
6 */
7
8 /** */
9 # Abort if called from a web server
10 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
11 print "This script must be run from the command line\n";
12 exit();
13 }
14
15 define('MEDIAWIKI',true);
16
17 # Process command line arguments
18 # $options becomes an array with keys set to the option names
19 # $optionsWithArgs is an array of GNU-style options that take an argument. The arguments are returned
20 # in the values of $options.
21 # $args becomes a zero-based array containing the non-option arguments
22
23 if ( !isset( $optionsWithArgs ) ) {
24 $optionsWithArgs = array();
25 }
26
27 $self = array_shift( $argv );
28 $self = __FILE__;
29 $IP = realpath( dirname( $self ) . '/..' );
30 chdir( $IP );
31
32 $options = array();
33 $args = array();
34
35
36 # Parse arguments
37
38 for( $arg = reset( $argv ); $arg !== false; $arg = next( $argv ) ) {
39 if ( substr( $arg, 0, 2 ) == '--' ) {
40 # Long options
41 $option = substr( $arg, 2 );
42 if ( in_array( $option, $optionsWithArgs ) ) {
43 $param = next( $argv );
44 if ( $param === false ) {
45 die( "$arg needs an value after it\n" );
46 }
47 $options[$option] = $param;
48 } else {
49 $bits = explode( '=', $option, 2 );
50 if( count( $bits ) > 1 ) {
51 $option = $bits[0];
52 $param = $bits[1];
53 } else {
54 $param = 1;
55 }
56 $options[$option] = $param;
57 }
58 } elseif ( $arg{0} == '-' ) {
59 # Short options
60 for ( $p=1; $p<strlen( $arg ); $p++ ) {
61 $option = $arg{$p};
62 if ( in_array( $option, $optionsWithArgs ) ) {
63 $param = next( $argv );
64 if ( $param === false ) {
65 die( "$arg needs an value after it\n" );
66 }
67 $options[$option] = $param;
68 } else {
69 $options[$option] = 1;
70 }
71 }
72 } else {
73 $args[] = $arg;
74 }
75 }
76
77
78 # General initialisation
79
80 $wgCommandLineMode = true;
81 # Turn off output buffering if it's on
82 @ob_end_flush();
83 $sep = PATH_SEPARATOR;
84
85 if (!isset( $wgUseNormalUser ) ) {
86 $wgUseNormalUser = false;
87 }
88
89 if ( file_exists( '/home/wikipedia/common/langlist' ) ) {
90 $wgWikiFarm = true;
91 require_once( $IP.'/includes/SiteConfiguration.php' );
92
93 # Get $conf
94 require( $IP.'/InitialiseSettings.php' );
95
96 if ( empty( $wgNoDBParam ) ) {
97 # Check if we were passed a db name
98 $db = array_shift( $args );
99 list( $site, $lang ) = $wgConf->siteFromDB( $db );
100
101 # If not, work out the language and site the old way
102 if ( is_null( $site ) || is_null( $lang ) ) {
103 if ( !$db ) {
104 $lang = 'aa';
105 } else {
106 $lang = $db;
107 }
108 if ( isset( $args[0] ) ) {
109 $site = array_shift( $args );
110 } else {
111 $site = 'wikipedia';
112 }
113 }
114 } else {
115 $lang = 'aa';
116 $site = 'wikipedia';
117 }
118
119 # This is for the IRC scripts, which now run as the apache user
120 # The apache user doesn't have access to the wikiadmin_pass command
121 if ( $_ENV['USER'] == 'apache' ) {
122 $wgUseNormalUser = true;
123 }
124
125 putenv( 'wikilang='.$lang);
126
127 $DP = $IP;
128 ini_set( 'include_path', ".:$IP:$IP/includes:$IP/languages:$IP/maintenance" );
129
130 require_once( $IP.'/includes/Defines.php' );
131 require_once( $IP.'/CommonSettings.php' );
132
133 if ( $wgUseRootUser ) {
134 $wgDBuser = $wgDBadminuser = 'root';
135 $wgDBpassword = $wgDBadminpassword = trim(`mysql_root_pass`);
136 } elseif ( !$wgUseNormalUser ) {
137 $wgDBuser = $wgDBadminuser = 'wikiadmin';
138 $wgDBpassword = $wgDBadminpassword = trim(`wikiadmin_pass`);
139 }
140 } else {
141 $wgWikiFarm = false;
142 $settingsFile = $IP.'/LocalSettings.php';
143
144 if ( ! is_readable( $settingsFile ) ) {
145 print "A copy of your installation's LocalSettings.php\n" .
146 "must exist in the source directory.\n";
147 exit();
148 }
149 $wgCommandLineMode = true;
150 $DP = $IP;
151 require_once( $IP.'/includes/Defines.php' );
152 require_once( $settingsFile );
153 ini_set( 'include_path', ".$sep$IP$sep$IP/includes$sep$IP/languages$sep$IP/maintenance" );
154
155 if ( is_readable( $IP.'/AdminSettings.php' ) ) {
156 require_once( $IP.'/AdminSettings.php' );
157 }
158 }
159
160 # Turn off output buffering again, it might have been turned on in the settings files
161 @ob_end_flush();
162 # Same with these
163 $wgCommandLineMode = true;
164
165 if ( empty( $wgUseNormalUser ) && isset( $wgDBadminuser ) && $wgDBservers ) {
166 $wgDBuser = $wgDBadminuser;
167 $wgDBpassword = $wgDBadminpassword;
168
169 foreach ( $wgDBservers as $i => $server ) {
170 $wgDBservers[$i]['user'] = $wgDBuser;
171 $wgDBservers[$i]['password'] = $wgDBpassword;
172 }
173 }
174
175 if ( defined( 'MW_CMDLINE_CALLBACK' ) ) {
176 $fn = MW_CMDLINE_CALLBACK;
177 $fn();
178 }
179
180 ini_set( 'memory_limit', -1 );
181
182 require_once( 'Setup.php' );
183 require_once( 'install-utils.inc' );
184 $wgTitle = Title::newFromText( 'Command line script' );
185 set_time_limit(0);
186
187 // --------------------------------------------------------------------
188 // Functions
189 // --------------------------------------------------------------------
190
191 function wfWaitForSlaves( $maxLag ) {
192 global $wgLoadBalancer;
193 if ( $maxLag ) {
194 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
195 while ( $lag > $maxLag ) {
196 $name = @gethostbyaddr( $host );
197 if ( $name !== false ) {
198 $host = $name;
199 }
200 print "Waiting for $host (lagged $lag seconds)...\n";
201 sleep($maxLag);
202 list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
203 }
204 }
205 }
206
207
208
209 ?>